package com.aurora.base.util;

import javax.servlet.http.HttpServletRequest;

public class PageContent {
	private int pageSize; // 每页显示的记录数
	private long recordCount; // 总共的记录数
	private int currentPage; // 当前页
	private int pageCount;// 总页数

	public PageContent() {
	}

	public PageContent(int pageSize, long recordCount, int currentPage) {
		this.currentPage = 1;
		this.setPageSize(pageSize);
		this.setRecordCount(recordCount);
		this.setCurrentPage(currentPage);
	}

	// 总页数
	public int getPageCount() {
		int size = (int) (this.recordCount / this.pageSize); // 总页=总条数/每页显示数
		int mod = (int) (this.recordCount % this.pageSize); // 最后一页的条数
		if (mod != 0) {
			size++;
		}
		return this.recordCount == 0 ? 1 : size;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getBeginRow() {
		return (this.currentPage - 1) * this.pageSize;
	}

	// 得到当前页
	public int getCurrentPage() {
		return this.currentPage;
	}

	// 设置当前页
	public void setCurrentPage(int currentPage) {
		int validPage = currentPage <= 0 ? 1 : currentPage;
		validPage = validPage > this.getPageCount()
				? this.getPageCount()
				: validPage;
		this.currentPage = validPage;
	}

	// 得到每页显示条数
	public int getPageSize() {
		return this.pageSize;
	}

	// 设置每页显示的条数
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	// 得到总共的条数
	public long getRecordCount() {
		return this.recordCount;
	}

	// 设置总共的条数
	public void setRecordCount(long recordCount) {
		this.recordCount = recordCount;
	}

	// 得到PageContent
	public static PageContent getPageContent(long recordCount,
			HttpServletRequest request) {
		String pageNum = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		int currentPage = 0;
		int count = 0; // 每页显示记录数
		if (pageNum == null || pageNum.trim().equals(""))
			currentPage = 1;
		else
			currentPage = Integer.parseInt(pageNum);
		if (pageSize == null || pageSize.trim().equals(""))
			count = 10; // 首次进入时显示的记录数
		else
			count = Integer.parseInt(pageSize);
		PageContent pageCnt = new PageContent(count, recordCount, currentPage);
		pageCnt.setPageCount(pageCnt.getPageCount());
		request.setAttribute("pageCnt", pageCnt);
		return pageCnt;
	}
}
